﻿using CommunityToolkit.Mvvm.ComponentModel;
using GlobalData;
using Microsoft.Extensions.DependencyInjection;
using Model.Core.Entity;
using Model.Core.Entity.Dbcontexts;
using Model.Core.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RobotPratolSolution.Models;
using System.Collections.ObjectModel;
using System.Windows;

namespace RobotPratolSolution.ViewModels
{
    public class StationMangeViewModel : ObservableObject
    {
        DbcontextBase _DbcontextBase { get; set; }
        MqTopics _GlobalMqTopics { get; set; }
        CloudMqClientCtrl _mqClientCtrl { get; set; }
        LocalRobotInfo LocalRobotInfo { get; set; }
        public StationMangeViewModel()
        {
            StationModels = new ObservableCollection<StationModelView>();
            _mqClientCtrl = App.ServiceProvider.GetService<CloudMqClientCtrl>();
            _DbcontextBase = App.ServiceProvider.GetService<DbcontextBase>();
            _GlobalMqTopics = App.ServiceProvider.GetService<MqTopics>();
            LocalRobotInfo = App.ServiceProvider.GetService<LocalRobotInfo>();

            _mqClientCtrl.AddSubTopic(_GlobalMqTopics.RobotPointStatusTopic);

            _mqClientCtrl.MsgReceivedEvent += (a, b) =>
            {

                if (_GlobalMqTopics.RobotPointStatusTopic.Equals(a))
                {
                    JObject jsondata = JsonConvert.DeserializeObject<JObject>(b);
                    X = Convert.ToDouble(jsondata["x"]);
                    Y = Convert.ToDouble(jsondata["y"]);
                    Z = Convert.ToDouble(jsondata["z"]);
                }

            };

            //using (_DbcontextBase = App.ServiceProvider.GetService<DbcontextBase>())
            //{

            //}
        }
        //private StationModelView _stationModelView;
        //public StationModelView stationModelView { get
        //    {
        //        return _stationModelView;
        //    }
        //    set
        //    {
        //       Set(ref _stationModelView, value);
        //    }

        //}

        public void DataListChanged()
        {

        }
               public void   AddSubTopic()
        {
                  _ = _mqClientCtrl.AddSubTopic(_GlobalMqTopics.RobotStatusTopic);
                  _ = _mqClientCtrl.AddSubTopic(_GlobalMqTopics.RobotPointStatusTopic);
        }

        public void RefreshStation()
        {
            StationModels.Clear();
            var datalist = _DbcontextBase.PointInfo?.Where(a => a.MapId != null && a.MapId.Id.ToString() == LocalRobotInfo.MapId)?.ToList();
            datalist?.ForEach(d =>
            {
                StationModels.Add(new StationModelView()
                {
                    Id = d.Id.ToString(),
                    Name = d.Name,
                    X = d.X,
                    Y = d.Y,
                    Z = d.Angle,
                    PointType = d.PointType,

                });
            });

        }
        public void AddStation(string Name, string type)
        {
            double pointx = PointX;
            double pointy = PointY;
            double pointz = PointZ;
            Guid guid = Guid.NewGuid();
            StationModels.Add(new StationModelView()
            {
                Name = Name,
                X = pointx,
                Y = pointy,
                Z = pointz,
                Id = guid.ToString(),
                IsChecked = true
            });
            Guid mapguid = Guid.NewGuid();
            try
            {
                mapguid = Guid.Parse(LocalRobotInfo.MapId);
            }
            catch (Exception)
            {

                MessageBox.Show("请先选择并应用地图");
                return;
            }
            var mapinfo = _DbcontextBase.MapInfo.FirstOrDefault(a => a.Id == mapguid);
            _DbcontextBase.PointInfo.Add(new Model.Core.Entity.PointInfo()
            {
                Name = Name,
                Id = guid,
                X = pointx,
                Y = pointy,
                Angle = pointz,
                PointType = type,
                MapId = mapinfo
            });

            _DbcontextBase.SaveChanges();
        }

        public void UpdateStation(int index)
        {
            var datainfo = StationModels[index];
            PointInfo pointInfo = _DbcontextBase.PointInfo.FirstOrDefault(a => a.Id.ToString() == datainfo.Id);
            if (pointInfo != null)
            {
                pointInfo.Name = PointName;
                pointInfo.X = PointX;
                pointInfo.Y = PointY;
                pointInfo.Angle = PointZ;
                pointInfo.PointType = PointType;
                _DbcontextBase.PointInfo.Update(pointInfo);
                datainfo.X = pointInfo.X;
                datainfo.Y = pointInfo.Y;
                datainfo.Z = pointInfo.Angle;
                datainfo.Name = pointInfo.Name;
                datainfo.PointType = pointInfo.PointType;
                _DbcontextBase.SaveChanges(true);
            }
        }

        public void StationGetPoint()
        {
            PointX = X;
            PointY = Y;
            PointZ = Z;

            // var angle = (PointZ / Math.PI) * 180;
            double Jackboxdistance = -0.23;
            if (PointType.ToLower() == "jack")
            {
                var datax = Math.Cos(PointZ);
                var datay = Math.Sin(PointZ);
                PointX = Math.Round(PointX + datax * Jackboxdistance, 4);
                PointY = Math.Round(PointY + datay * Jackboxdistance, 4);


            }
        }

        public void DoMovePoint(int pointtype)
        {
            JObject jsondata = new JObject();




            jsondata.Add("x", PointX);
            jsondata.Add("y", PointY);
            jsondata.Add("z", PointZ);
            jsondata.Add("type", (int)pointtype);
            _mqClientCtrl.SendMessageAsync(_GlobalMqTopics.MoveToPoint_Topic, jsondata.ToString());
        }
        public void DoUpdateToRobot()
        {
            JObject jsondata = new JObject();
            jsondata.Add("command", "UpdateMapPointWithJack");
            _mqClientCtrl.SendMessageAsync(_GlobalMqTopics.Map_UpdateJack, jsondata.ToString());
        }



        public void DoChargeFunc()
        {
            //var datainfo = StationModels[index];
            //PointInfo pointInfo = _DbcontextBase.PointInfos.FirstOrDefault(a => a.Id.ToString() == datainfo.Id);
            //if (pointInfo != null)
            //{
            //    _DbcontextBase.PointInfos.Remove(pointInfo);
            //    StationModels.RemoveAt(index);
            //}
            JObject jsondata = new JObject();
            _mqClientCtrl.SendMessageAsync(_GlobalMqTopics.DoChargeTopic, jsondata.ToString());
        }

        public void DeleteStation(int index)
        {
            var datainfo = StationModels[index];
            PointInfo pointInfo = _DbcontextBase.PointInfo.FirstOrDefault(a => a.Id.ToString() == datainfo.Id);
            if (pointInfo != null)
            {
                _DbcontextBase.PointInfo.Remove(pointInfo);
                StationModels.RemoveAt(index);
                _DbcontextBase.SaveChanges();
            }
        }
        public ObservableCollection<StationModelView> StationModels { get; set; }



        private double _PointX;
        public double PointX
        {
            get { return _PointX; }
            set {  _PointX = value; OnPropertyChanged(); }
       
        }

        private double _PointY;
        public double PointY
        {
            get { return _PointY; }
            set {  _PointY = value; OnPropertyChanged(); }
        
        }
        private double _PointZ;
        public double PointZ
        {
            get { return _PointZ; }
            set {  _PointZ = value; OnPropertyChanged(); }
        
        }




        private string _MapId;
        public string MapId
        {
            get { return _MapId; }
            set { _MapId = value; OnPropertyChanged(); }
        }
        private string _MapName;
        public string MapName
        {
            get { return _MapName; }
            set {  _MapName = value; OnPropertyChanged(); }
        }




        private string _PointName;
        public string PointName
        {
            get { return _PointName; }
            set {  _PointName = value;
                OnPropertyChanged(); }
        }

        private string _PointType;
        public string PointType
        {
            get { return _PointType; }
            set {  _PointType = value;
                OnPropertyChanged();
            }
        }


        private double _X;
        public double X
        {
            get { return _X; }
            set {  _X = value; OnPropertyChanged(); }
        }

        private double _Y;
        public double Y
        {
            get { return _Y; }
            set {  _Y = value; OnPropertyChanged(); }
        }
        private double _Z;
        public double Z
        {
            get { return _Z; }
            set {  _Z = value; OnPropertyChanged(); }
        }
    }

    public class StationModelView : StationModel
    {
        private bool _IsChecked;
        public bool IsChecked
        {
            get { return _IsChecked; }
            set { _IsChecked = value; OnPropertyChanged(); }
        }
    }
}
